<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Alarm\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Database\Model\Builder;
use Plugin\Alarm\Model\RdAlarmHandleRecord;

/**
 * 报警处置记录仓库类
 */
class RdAlarmHandleRecordRepository extends IRepository
{
    public function __construct(
        protected readonly RdAlarmHandleRecord $model
    )
    {
    }

    public function handleSearch(Builder $query, array $params): Builder
    {
        if (Arr::get($params, 'work_id')) {
            $query->whereIn('work_id', Arr::wrap($params['work_id']));
        }

        return $query;
    }

    public function transferHandle(int $workId, int $userId, array $params)
    {
        $this->apiCreate($workId, $userId, 2, $params);
    }

    public function apiCreate($workId, $userId, $handle, $params)
    {
        return $this->create([
            'work_id' => $workId,
            'user_id' => $userId,
            'handle_type' => $handle,
            'handle_info' => $params,
        ]);
    }

    public function acceptTransferHandle(int $workId, int $userId)
    {
        $this->apiCreate($workId, $userId, 3, []);
    }

    public function createHandle(int $workId, int $userId)
    {
        $this->apiCreate($workId, $userId, 1, []);
    }

    public function readHandle($id, mixed $updated_by)
    {
        if ($this->getQuery()
            ->where('work_id', $id)
            ->where('user_id', $updated_by)
            ->where('handle_type', 4)->exists()) {
            return true;
        }
        return $this->apiCreate($id, $updated_by, 4, []);
    }

    public function closeHandle(int $id, int $updated_by, $params)
    {
        return $this->apiCreate($id, $updated_by, 5, $params);
    }

    public function reviewHandle(int $id, int $updated_by, array $array)
    {
        return $this->apiCreate($id, $updated_by, 6, $array);
    }

}